package com.sgb.jicai.view.activity;

import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.sgb.jicai.R;
import com.sgb.jicai.databinding.ActivityFactoryCentralizedBinding;
import com.sgb.jicai.view.fragment.FactoryCentralizedFragment;
import com.sgb.jicai.viewmodel.FactoryCentralizedModel;
import com.sgb.link.entity.EventConstant;
import com.swgk.core.base.AppActivity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.jicai.event.JiCaiEventEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.List;
/***
 * 集采首页--集采--供方发起 activity
 * @author Administrator
 */

public class FactoryCentralizedActivity extends AppActivity {
    FactoryCentralizedModel mModel;
    ActivityFactoryCentralizedBinding mBind;

    public static void start(Context context) {
        context.startActivity(new Intent(context, FactoryCentralizedActivity.class));
    }
    @Override
    public void onMultiClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.tv_search) {//调用Fragment的刷新
            EventBus.getDefault().post(new JiCaiEventEntity(EventConstant.EVENT_JICAI_HOME_FACTORY, mBind.editSearch.getText().toString().trim()));
        }
    }

    @Override
    protected void initView() {
        mBind = DataBindingUtil.setContentView(this, R.layout.activity_factory_centralized);
        mModel = new ViewModelProvider(this).get(FactoryCentralizedModel.class);
        mBind.ivBack.setOnClickListener(this::onMultiClick);
        mBind.tvSearch.setOnClickListener(this::onMultiClick);
      //  updateFactoryJiCaiViews();//此方法之后删除
        mBind.editSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    //调用Fragment的刷新
                    EventBus.getDefault().post(new JiCaiEventEntity(EventConstant.EVENT_JICAI_HOME_FACTORY, mBind.editSearch.getText().toString().trim()));
                }
                return false;
            }
        });
    }

    @Override
    protected void init() {
        mModel.groupBuyClassList(this);
    }

    public void showEmpty() {
        mBind.emptyView.setVisibility(View.VISIBLE);
    }

    public void updateViews(List<PickSelectEntity> data) {
        if (data == null || data.size() == 0) {
            DialogHelper.dismissProgressDialog();
            showEmpty();
            return;
        }
        String[] mTitles = new String[data.size()];
        ArrayList<Fragment> mFragments = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {//类型：商品集采、机械集采、设备集采…………
            mTitles[i] = data.get(i).getPickerViewText();
            mFragments.add(FactoryCentralizedFragment.newInstance(data.get(i).getNo().isEmpty() ? 0 : Integer.valueOf(data.get(i).getNo()), "1"));
        }
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mBind.viewPager.setAdapter(mPagerAdapter);
        mBind.tabLayout.setupWithViewPager(mBind.viewPager);
       // mBind.tabLayout.setTabMode(data.size() > 3 ? TabLayout.MODE_SCROLLABLE : TabLayout.MODE_FIXED);
        mBind.tabLayout.setVisibility(View.GONE);
    }

    //厂家集采分类开发后此方法删除
/*
    public void updateFactoryJiCaiViews() {
        String[] mTitles = new String[1];
        mTitles[0] = "商品集采";
        ArrayList<Fragment> mFragments = new ArrayList<>();
        mFragments.add(FactoryCentralizedFragment.newInstance(1, "1"));//现在厂家集采只有商品，固定传1
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mBind.viewPager.setAdapter(mPagerAdapter);
        mBind.tabLayout.setupWithViewPager(mBind.viewPager);
        mBind.tabLayout.setVisibility(View.GONE);//厂家集采暂时无一级分类信息
    }
*/


}
